package edu.ccut.saturn.studio.server.tomcat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.launching.JavaRuntime;

import edu.ccut.saturn.studio.server.model.SaturnServer;
import edu.ccut.saturn.studio.server.model.SaturnServerModel;

public class TomcatLauncherPlugin  {

	static final String TOMCAT_VERSION5 = "tomcatV5";
	static final String TOMCAT_VERSION6 = "tomcatV6";
	static final String TOMCAT_PREF_CONTEXTSDIR_KEY = "contextsDir";
				
	private static final String TOMCAT_HOME_CLASSPATH_VARIABLE = "TOMCAT_HOME";
	private static String startupPath = "";
	
	/**
	 * The constructor.
	 */
	public TomcatLauncherPlugin() {
	}

	public static String getTomcatDir() {
		return startupPath;
	}

	public static String getTomcatBase() {
		return startupPath;
	}

	public static String getConfigFile() {
		return startupPath + File.separator + "conf\\server.xml";
	}

	public String getConfigMode() {
		return "serverFile";
	}
	
	public static String getTomcatVersion() {
		return	TOMCAT_VERSION5;
	}

	public static String getTomcatJRE() {
		return JavaRuntime.getDefaultVMInstall().getId();
	}
	
	public static boolean isDebugMode() {
	    return true;
	}

	public static TomcatBootstrap getTomcatBootstrap() {
		TomcatBootstrap tomcatBootsrap = null;
		
		if(getTomcatVersion().equals(TOMCAT_VERSION5)) {
			tomcatBootsrap = new Tomcat5Bootstrap();				
		}
//		if(getTomcatVersion().equals(TOMCAT_VERSION6)) {
//			tomcatBootsrap = new Tomcat6Bootstrap();				
//		}
				
		return tomcatBootsrap;
	}

	
    public static String readTextFile(File f) throws IOException {
		
		StringBuffer buf = new StringBuffer();

		BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
		String inputLine;
		while ((inputLine = in.readLine()) != null) {
			buf.append(inputLine);
			buf.append('\n');
		}
		
		in.close();
		return buf.toString();
	}
    
	public static void toTextFile(File f, String content) throws IOException {
		FileOutputStream out = new FileOutputStream(f);
		out.write(content.getBytes("UTF-8"));
		out.close(); 		
	}

	
	public static IPath getTomcatIPath() {
		IPath tomcatPath = getTomcatClasspathVariable();
		if(tomcatPath == null) {
			return new Path(TomcatLauncherPlugin.getTomcatDir());
		} else {
			return new Path(TOMCAT_HOME_CLASSPATH_VARIABLE);
		}	
	}
	
	private static IPath getTomcatClasspathVariable() {
		IPath tomcatPath = JavaCore.getClasspathVariable(TOMCAT_HOME_CLASSPATH_VARIABLE);
		if(tomcatPath == null) {
			TomcatLauncherPlugin.initTomcatClasspathVariable();
			tomcatPath = JavaCore.getClasspathVariable(TOMCAT_HOME_CLASSPATH_VARIABLE);
		}
		return tomcatPath;
	}

	public static void initTomcatClasspathVariable() {
		try {
			JavaCore.setClasspathVariable(
				TOMCAT_HOME_CLASSPATH_VARIABLE,
				new Path(TomcatLauncherPlugin.getTomcatDir()),
				null);		
		} catch (JavaModelException e) {
			e.printStackTrace();
		}		
	}
	
	
	static public boolean isTomcatConfigured() {
		return !(TomcatLauncherPlugin.getTomcatDir().equals(""));
	}
	
	public static void setconfig() {
		 Map<String, SaturnServer> saturnServers = SaturnServerModel.getSaturnServers();
		 SaturnServer server = new SaturnServer();
		 Set<String> keys = saturnServers.keySet();
		 for (String key : keys) {
			 
			if(!"".equals(key)) {
				server = saturnServers.get(key);
			}
		}
		startupPath = server.getServerPath(); 
		 
	}
}
